/**
 * 
 */
package upload;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Think
 * 
 */
public class FileUploadAction extends ActionSupport {
	// buffer step defind
	private static int BUFFER_SIZE = 16 * 1024;
	// file
	private File myFile;
	// file type
	private String myFileContentType;
	// file name
	private String myFileFileName;
	// image file name
	private String imageFileName;
	// title
	private String caption;

	/**
	 * @return the myFile
	 */
	public File getMyFile() {
		return myFile;
	}

	/**
	 * @param myFile
	 *            the myFile to set
	 */
	public void setMyFile(File myFile) {
		this.myFile = myFile;
	}

	/**
	 * @return the contentType
	 */
	public String getMyFileContentType() {
		return myFileContentType;
	}

	/**
	 * @param contentType
	 *            the contentType to set
	 */
	public void setMyFileContentType(String contentType) {
		this.myFileContentType = contentType;
	}

	/**
	 * @return the fileName
	 */
	public String getMyFileFileName() {
		return myFileFileName;
	}

	/**
	 * @param fileName
	 *            the fileName to set
	 */
	public void setMyFileFileName(String fileName) {
		this.myFileFileName = fileName;
	}

	/**
	 * @return the imageFileName
	 */
	public String getImageFileName() {
		return imageFileName;
	}

	/**
	 * @param imageFileName
	 *            the imageFileName to set
	 */
	public void setImageFileName(String imageFileName) {
		this.imageFileName = imageFileName;
	}

	/**
	 * @return the caption
	 */
	public String getCaption() {
		return caption;
	}

	/**
	 * @param caption
	 *            the caption to set
	 */
	public void setCaption(String caption) {
		this.caption = caption;
	}

	// 读取文件操作，读文件流和写文件流
	private static void copy(File src, File dst) {
		try {
			InputStream in = null;
			OutputStream out = null;
			try {
				in = new BufferedInputStream(new FileInputStream(src));
				out = new BufferedOutputStream(new FileOutputStream(dst));
				byte[] buffer = new byte[BUFFER_SIZE];

				while (in.read(buffer) > 0) {
					out.write(buffer);
				}

			} finally {
				if (null != in) {
					in.close();
				}
				if (null != out) {
					out.close();
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	// 解析文件扩展名
	private static String getExtention(String fileName) {
		int pos = fileName.lastIndexOf(".");
		return fileName.substring(pos);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ActionSupport#execute()
	 */
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		imageFileName = new Date().getTime() + getExtention(myFileFileName);
		File imageFile = new File(ServletActionContext.getServletContext()
				.getRealPath("/upload/UploadImages")
				+ "/" + imageFileName);
		copy(myFile, imageFile);
		return this.SUCCESS;
	}

}
